<template>
  <div>
    <login-top middleTop="登录">
      <div slot="right" style="font-size: 3vw" @click="$router.push('/register')">点此注册</div>
    </login-top>
    <login-text @inputChange="res => username = res" rule="^.{6,16}$" label="账号" placeholder="请输入账号"></login-text>
    <login-text @inputChange="res => password = res" rule="^.{6,16}$" label="密码" type="password" placeholder="请输入密码"></login-text>
    <login-btn btntext="登录" @registerSubmit="registerSubmit"></login-btn>
  </div>
</template>

<script>
  import LoginTop from '@/components/common/LoginTop.vue'
  import LoginText from '@/components/common/LoginText.vue'
  import LoginBtn from '@/components/common/LoginBtn.vue'
  export default {
    components:{
      LoginTop,
      LoginText,
      LoginBtn
    },
    name: "register",
    data() {
      return {
        username:'',
        password:''
      }
    },
    methods: {
      async registerSubmit(){
        let rulg = /^.{6,16}$/
        let model = {
          username:this.username,
          password:this.password
        }
        if (rulg.test( this.name) && rulg.test(this.password) ) {
          const res = await this.$http.post('/login',model)
          this.$msg.fail(res.data.msg)
          if (res.data.code == 200){
            setTimeout(() =>{
              localStorage.setItem('token',res.data.token)
              localStorage.setItem('id',res.data.id)
              this.$router.push('/userinfo')
            },1000)
          }

          console.log(res)
        }else{
          this.$msg.fail("格式不正确")
        }
      }
    }
  }
</script>

<style scoped>

</style>
